//*****************************************************************************
// file        : ad2428_pal.c
// ad2428 pal implementations.
// Copyright (c) 2024-2024 Dynaudio Technology co. Ltd. All rights reserved
//
// Change Logs:
// Date                 Author      Note
// 2024/03/01           Cheney      First draft version
//
//*****************************************************************************

//*****************************************************************************
//
//! \addtogroup ad2428
//! @{
//
//*****************************************************************************
#include "ad2428_pal.h"
#include "a2b_cfg.h"
// #include "hal_i2c.h"

// #define AD2428_I2C_INDEX            HAL_INDEX_LPI2C1

// static hal_i2c_instance_t *ad2428_i2c_instance;
// static SemaphoreHandle_t ad2428_mutex;

/**
 * @brief ad2428 pal i2c init.
 *
 * @return operation result.
 */
int ad2428_i2c_init(void)
{
    // ad2428_i2c_instance = hal_i2c_get_instance(AD2428_I2C_INDEX);
    // ad2428_mutex = xSemaphoreCreateMutex();

    // if (ad2428_mutex == NULL)
    // {
    //     return -1;
    // }

    return 0;
}

/**
 * @brief ad2428 pal i2c deinit.
 *
 * @return operation result.  
 */
int ad2428_i2c_deinit(void)
{
    // vSemaphoreDelete(ad2428_mutex);
    // ad2428_mutex = NULL;
    return 0;
}

/**
 * @brief write data to device
 *
 * @param address device address
 * @param nWrite length in bytes of the data to be transferred
 * @param wbuf pointer to the data to be transferred
 * @return Error or success status returned by API
 */
int ad2428_i2c_write(uint16_t address, uint16_t nWrite, uint8_t *wbuf)
{
    int ret_val = -1;

    // if (ad2428_mutex != NULL)
    // {
    //     if (xSemaphoreTake(ad2428_mutex, portMAX_DELAY) == pdTRUE)
    //     {
    //         if (hal_i2c_master_send_blocking(ad2428_i2c_instance, address, wbuf, nWrite, 1, portMAX_DELAY) == STATUS_SUCCESS)
    //         {
    //             ret_val = 0;
    //         }

    //         xSemaphoreGive(ad2428_mutex);
    //     }
    // }

    return ret_val;
}

/**
 * @brief write and read data from device.
 *
 * @param address device address
 * @param nWrite length in bytes of the data to be transferred
 * @param wbuf pointer to the data to be transferred
 * @param nRead length in bytes of the data to receive
 * @param rbuf pointer to the space to save the received data.
 * @return Error or success status returned by API  
 */
int ad2428_i2c_write_read(uint16_t address, uint16_t nWrite, uint8_t *wbuf, uint16_t nRead, uint8_t *rbuf)
{
    int ret_val = -1;

    // if (ad2428_mutex != NULL)
    // {
    //     if (xSemaphoreTake(ad2428_mutex, portMAX_DELAY) == pdTRUE)
    //     {
    //         if (hal_i2c_master_send_blocking(ad2428_i2c_instance, address, wbuf, nWrite, 0, portMAX_DELAY) == STATUS_SUCCESS)
    //         {
    //             if (hal_i2c_master_receive_blocking(ad2428_i2c_instance, address, rbuf, nRead, 1, portMAX_DELAY) == STATUS_SUCCESS)
    //             {
    //                 ret_val = 0;
    //             }
    //         }

    //         xSemaphoreGive(ad2428_mutex);
    //     }
    // }

    return ret_val;
}

/**
 * @brief delay x ms
 *
 * @param nTime the delay time(unit: ms)
 */
void ad2428_delay(uint32_t nTime)
{
    // vTaskDelay(pdMS_TO_TICKS(nTime));
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************